#include "dma.h"
#include "tim.h"

#include "WS2812.h"

uint16_t RGB_buffur[RESET_PULSE + WS2812_DATA_LEN] = { 0 };

/// @brief WS2812设置LED颜色
/// @param R red 
/// @param G green
/// @param B blue
/// @param num 通道数，即亮灯LED编号
void WS2812SetColor(uint8_t R, uint8_t G, uint8_t B, uint16_t num)
{
    
    uint16_t* p = (RGB_buffur + RESET_PULSE) + (num * LED_DATA_LEN);
    
    for (uint16_t i = 0;i < 8;i++)
    {
       
        p[i]      = (G << i) & (0x80)?ONE_PULSE:ZERO_PULSE;
        p[i + 8]  = (R << i) & (0x80)?ONE_PULSE:ZERO_PULSE;
        p[i + 16] = (B << i) & (0x80)?ONE_PULSE:ZERO_PULSE;
    }
}

/// @brief 重置ws2812各led灯
/// @param  
void WS2812Reset(void)
{
	WS2812SetColor(0x00, 0x00, 0x00, 0); 
	WS2812SetColor(0x00, 0x00, 0x00, 1);
	WS2812SetColor(0x00, 0x00, 0x00, 2);
	WS2812SetColor(0x00, 0x00, 0x00, 3);
	WS2812SetColor(0x00, 0x00, 0x00, 4);
	WS2812SetColor(0x00, 0x00, 0x00, 5);
	WS2812SetColor(0x00, 0x00, 0x00, 6);
}

/// @brief 将设置好的led信号发送到ws2812中
/// @param  
void WS2812SignalSend(void)
{
	HAL_TIM_PWM_Start_DMA(&htim8,TIM_CHANNEL_2,(uint32_t *)RGB_buffur,DMA_LEN);
}
